/*
 * CS_ULTIMATE_EVOLUTION_BATTLE_PET.cpp
 *
 *  Created on: 2013. 8. 12.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Item.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand {

void SendFailBattlepetEvolution(User * pUser, int res)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(res);
	pAck->MakePacket(CMD_SC_ULTIMATE_EVOLUTION_BATTLE_PET);
	pUser->Send(pAck);
}

void Command::CS_ULTIMATE_EVOLUTION_BATTLE_PET(User * pUser, ::Network::Packet * pPacket)
{
/*	//전투펫 궁극진화
	CS_ULTIMATE_EVOLUTION_BATTLE_PET	0x3004
	{
		u64 u64base_pet;			//진화시킬 전투펫
		u64 u64material_1;			//재료 1 아이템
		u8	u8material_1_count;		//재료 1 갯수
		u64 u64material_2;			//재료 2 아이템
		u8	u8material_2_count;		//재료 2 갯수
	}
	SC_ULTIMATE_EVOLUTION_BATTLE_PET	0x3005
	{
		s16	u16ack;				//1: 진화 성공 (진화는 실패 없음)
	}*/

	PREPARE_PACKET;

	uint64_t basePetSeq = 0;
	uint64_t materialSeq = 0;
	uint64_t materialSeq2 = 0;
	char materialCount = 0;
	char materialCount2 = 0;

	*pPacket >> basePetSeq >> materialSeq >> materialCount >> materialSeq2 >> materialCount2;

//	DEBUGLOG("CS_ULTIMATE_EVOLUTION_BATTLE_PET GID=%d, base_seq=%lld, material_seq1=%lld, material_count1=%d, material_seq2=%lld, material_count2=%d",
//			basePetSeq, materialSeq, materialCount, materialSeq2, materialCount2);

	NLogic::Bag * pBag = pUser->GetBag(BAG_TYPE_BATTLE_PET);
	NLogic::Bag * pBagMisc = pUser->GetBag(BAG_TYPE_MISC);

	if (pBag == NULL || pBagMisc == NULL)
	{
		SendFailBattlepetEvolution(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NLogic::Item * pItemBase		= pBag->GetItem(basePetSeq);
	NLogic::Item * pItemMaterial	= pBagMisc->GetItem(materialSeq);
	NLogic::Item * pItemMaterial2	= pBagMisc->GetItem(materialSeq2);

	if (pItemBase == NULL || pItemMaterial == NULL || pItemMaterial2 == NULL)
	{
		SendFailBattlepetEvolution(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NLogic::Item::Data & clsData			= pItemBase->GetData();
	NLogic::Item::Data & clsDataMaterial	= pItemMaterial->GetData();
	NLogic::Item::Data & clsDataMaterial2	= pItemMaterial2->GetData();

	int ack = NResource::Static::InstancePtr()->GetBattlepetGachya()->BattlePetEvolution(pUser, clsData, clsDataMaterial, materialCount, clsDataMaterial2, materialCount2);

	// 진화에 성공하면 재료 차감
	if (ACK_OK == ack)
	{
		pItemBase->UpdateDatabaseItemInfo();

		if (clsDataMaterial.quantity <= 0) {
			pBagMisc->PopItem(materialSeq);
			pItemMaterial->DeleteDatabaseItemInfo(NLogic::Item::ItemStatus::EM_ITEM_STATUS_BATTLEPET_EVOLUTION);
		} else {
			pItemMaterial->UpdateDatabaseItemInfo();
		}

		if (clsDataMaterial2.quantity <= 0) {
			pBagMisc->PopItem(materialSeq2);
			pItemMaterial2->DeleteDatabaseItemInfo(NLogic::Item::ItemStatus::EM_ITEM_STATUS_BATTLEPET_EVOLUTION);
		} else {
			pItemMaterial2->UpdateDatabaseItemInfo();
		}
	}

	// 응답
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_ULTIMATE_EVOLUTION_BATTLE_PET);
	pUser->Send(pAck);
}

} /* NCommand */ } /* ProjectZ */
